# 改变duty circle 实现颜色的变化
import RPi.GPIO as GPIO
import time

def change_dt(color_list):
    '''
     将0-255更改为0-100并返回
    '''
    color_list = list(color_list)
    for i in range(len(color_list)):
        if color_list[i]<0:
            color_list[i]=0
        elif color_list[i]>255:
            color_list[i]=255
    return int(100-(255-color_list[0])/255*100),int(100-(255-color_list[1])/255*100),int(100-(255-color_list[2])/255*100)

def start():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setwarnings(False)
    # 11:红 12:绿 13:蓝
    GPIO.setup((11,12,13),GPIO.OUT)
    frequency_green = 2000
    frequency_red = 2000
    frequency_blue = 5000
    global pwm_list
    pwm_list = [GPIO.PWM(11,frequency_green), GPIO.PWM(12,frequency_red), GPIO.PWM(13,frequency_blue)]

def end():
    for p in pwm_list:
        p.stop()
    GPIO.cleanup()


def loop(colors):
    for p in pwm_list:
        p.start(0)
    dc_list=change_dt(colors)
    while True:
        for i in range(3):
            pwm_list[i].ChangeDutyCycle(dc_list[i])
        time.sleep(1)

if __name__ == '__main__':
    color =255,255,0
    colors=change_dt((color))
    print(colors)
    try:
        start()
        loop(colors)
    except KeyboardInterrupt:
        print('bye')
        end()